// Package editvois impliments the view model for the vois markup editor and its pages.
package editvois

import (
	"fmt"
	"html/template"
	"net/url"
	"time"

	"code.google.com/awwwe-browser/auralvm/param"
	"code.google.com/awwwe-browser/data"
)

var (
	Scheme   string
	Host     string
	Port     int
	Package  string
	HomeURL  string
	TestURL  string
	AWWWEURL string
)

// returns host and port formatted for a url.
func HostPort() string {
	return fmt.Sprintf("%s:%d", Host, Port)
}

// sets editvois.Scheme, editvois.Host, editvois.Port, editvois.Package for building urls for this package.
func SetSchemeHostPortPackage(scheme, host string, port int, pack string) {
	var u url.URL
	Host = host
	Port = port
	Scheme = scheme
	Package = pack
	u.Scheme = scheme
	u.Host = HostPort()
	u.Path = "/test"
	TestURL = u.String()
	u.Path = "/awwwe"
	AWWWEURL = u.String()
	u.Path = fmt.Sprintf("/%s/", Package)
	HomeURL = u.String()
}

var (
	currentOnClick       template.JS = template.JS("return false")
	leavingEditorOnClick template.JS = template.JS("return confirm('If you exit the VOIS Editor you will lose all of your edits.\\r\\nClick Ok to exit the VOIS Editor.');")
	homeCssUrl           string      = "%sStyles/Home"
	notHomeCssUrl        string      = "%sStyles/NotHome"
	navCssUrl            string      = "%sStyles/Navigation"
	generalCssUrl        string      = "%sStyles/General"
	normalFormCssUrl     string      = "%sStyles/Form"
	homeStyleFunc        string      = "StyleWriter.writeHomeStyles();"
	notHomeStyleFunc     string      = "StyleWriter.writeNotHomeStyles();"
	formStyleFunc        string      = "StyleWriter.writeFormStyles();"
)

// view data
type PageData struct {
	Date                 string
	HomeURL              template.URL
	TestURL              template.URL
	AWWWEURL             template.URL
	PathFolder, PathPage string
	PageTitle            string
	Layout               string
	PageClass            string
	HeaderClass          string
	BodyClass            string
	MainContentClass     string
	NavSection           string
	// error message
	ErrorMessage template.HTML
	// css and styles
	CSSURL        template.URL
	FormCSSURL    template.URL
	NavCSSURL     template.URL
	GeneralCSSURL template.URL
	StyleFunc     string
	FormStyleFunc string
	// link classes
	HomeLinkClass              string
	VoisEditorLinkClass        string
	TrueKissLinkClass          string
	HowToVoisIntroClass        string
	HowToVoisLessonsClass      string
	HowToNavigationSpacesClass string
	HowToArticleSpacesClass    string
	HowToFormSpacesClass       string
	HowToVoisEditorPart1Class  string
	HowToVoisEditorPart2Class  string
	HowToXpathIntroClass       string
	HowToVoisExamplesClass     string
	TestBrowserLinkClass       string
	AWWWEBrowserLinkClass      string
	/*
		TouLinkClass                     string
		MessageLinkClass                 string
		BrowserVidWatchBMLinkClass       string
		BrowserVidVoiceCommandsLinkClass string
		BrowserVidDictationLinkClass     string
		BrowserVidIntroLinkClass         string
	*/
	// on click
	HomeOnClick                  template.JS
	VoisEditorOnClick            template.JS
	TrueKissOnClick              template.JS
	HowToVoisIntroOnClick        template.JS
	HowToVoisLessonsOnClick      template.JS
	HowToNavigationSpacesOnClick template.JS
	HowToArticleSpacesOnClick    template.JS
	HowToFormSpacesOnClick       template.JS
	HowToVoisEditorPart1OnClick  template.JS
	HowToVoisEditorPart2OnClick  template.JS
	HowToXpathIntroOnClick       template.JS
	HowToVoisExamplesOnClick     template.JS
	TestBrowserOnClick           template.JS
	AWWWEBrowserOnClick          template.JS
	/*
		TouOnClick                     template.JS
		MessageOnClick                 template.JS
		FeaturesOnClick                template.JS
		DeveloperOnClick               template.JS
		SpecificationsOnClick          template.JS
		NewsOnClick                    template.JS
		BrowserVidWatchBMOnClick       template.JS
		BrowserVidVoiceCommandsOnClick template.JS
		BrowserVidDictationOnClick     template.JS
		BrowserVidIntroOnClick         template.JS
		DownloadOnClick                template.JS
		HowToVoisEditorWhatsNewOnClick template.JS
	*/
	// markup
	HeadMarkup       template.HTML
	NavigationMarkup template.HTML
	BodyMarkup       template.HTML
	EditableMarkup   template.HTML
	// form data
	VOISXMLSaveURL template.URL
	EditURL        template.HTMLAttr
	Host           template.HTMLAttr
	VoisMarkup     template.HTMLAttr
	// param names
	ParamVoisMarkup       template.HTMLAttr
	ParamResponsdWithText template.HTMLAttr
	// general params : name : value pairs
	NameValuePairs map[template.HTMLAttr]template.HTMLAttr
}

// sets the onclicks for the view data.
func (this *PageData) setOnClicks(onclick template.JS) {
	this.HomeOnClick = onclick
	this.VoisEditorOnClick = onclick
	this.TrueKissOnClick = onclick
	this.HowToNavigationSpacesOnClick = onclick
	this.HowToArticleSpacesOnClick = onclick
	this.HowToFormSpacesOnClick = onclick
	this.HowToVoisLessonsOnClick = onclick
	this.HowToVoisIntroOnClick = onclick
	this.HowToVoisEditorPart1OnClick = onclick
	this.HowToVoisEditorPart2OnClick = onclick
	this.HowToXpathIntroOnClick = onclick
	this.HowToVoisExamplesOnClick = onclick
	this.TestBrowserOnClick = onclick
	this.AWWWEBrowserOnClick = onclick
	/*
		this.MessageOnClick = onclick
		this.FeaturesOnClick = onclick
		this.ProjectOnClick = onclick
		this.TouOnClick = onclick
		this.NewsOnClick = onclick
		this.BrowserVidWatchBMOnClick = onclick
		this.BrowserVidVoiceCommandsOnClick = onclick
		this.BrowserVidDictationOnClick = onclick
		this.DownloadOnClick = onclick
		this.HowToVoisEditorWhatsNewOnClick = onclick
	*/
}

// builds new view data.
func newPageData(pathfolder, pathpage string) *PageData {
	var v PageData
	var t time.Time
	v.HomeURL = template.URL(HomeURL)
	v.TestURL = template.URL(TestURL)
	v.AWWWEURL = template.URL(AWWWEURL)
	v.PathFolder = pathfolder
	v.PathPage = pathpage
	t = time.Now()
	v.Date = fmt.Sprintf("%d", t.Year())
	v.VoisEditorLinkClass = "voisProjectSectionLink"
	/*
		v.ProjectLinkClass = "voisProjectSectionLink"
		v.TouLinkClass = "voisProjectSectionLink"
	*/
	v.setOnClicks(template.JS(""))
	v.VOISXMLSaveURL = template.URL(fmt.Sprintf("%s://%s/%s/vois/save", Scheme, param.HostPort(), param.Package))
	v.ParamVoisMarkup = template.HTMLAttr(param.ParamVoisMarkup)
	v.ParamResponsdWithText = template.HTMLAttr(param.ParamResponsdWithText)
	v.NameValuePairs = make(map[template.HTMLAttr]template.HTMLAttr)
	v.TestBrowserLinkClass = data.EmptyString
	v.AWWWEBrowserLinkClass = data.EmptyString
	return &v
}
